package com.cnzz.site1255174697.fragment.main;

import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.alibaba.baichuan.android.trade.AlibcTrade;
import com.alibaba.baichuan.android.trade.callback.AlibcTradeCallback;
import com.alibaba.baichuan.android.trade.constants.AlibcConstants;
import com.alibaba.baichuan.android.trade.model.AlibcShowParams;
import com.alibaba.baichuan.android.trade.model.OpenType;
import com.alibaba.baichuan.android.trade.model.TradeResult;
import com.alibaba.baichuan.android.trade.page.AlibcBasePage;
import com.alibaba.baichuan.android.trade.page.AlibcPage;
import com.cnzz.site1255174697.Const;
import com.cnzz.site1255174697.R;
import com.cnzz.site1255174697.utils.SystemBarUtils;
import com.cnzz.site1255174697.utils.WebViewUtils;
import com.cqyanyu.yanyu.model.EventEntity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

/**
 * 我的
 */
public class MyFragment extends Fragment {
    private View rootView;
    private WebView webView;
    private boolean isEvent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_cart, null);
        webView = (WebView) rootView.findViewById(R.id.mWebView);
        SystemBarUtils.setSystemBarColor(getActivity(), rootView);
        initView();
        return rootView;
    }

    //初始化
    private void initView() {
        WebViewUtils.seWebSettingst(webView);
        webView.loadUrl(Const.LOGIN_URL);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("http:") || url.startsWith("https:")) {
                    if (url.startsWith("https://h5.m.taobao.com/mlapp/cart.html")) {
                        EventEntity eventEntity = new EventEntity();
                        eventEntity.type = Const.JUMP_CART;
                        EventBus.getDefault().post(eventEntity);
                        webView.loadUrl(Const.LOGIN_URL);
                        return true;
                    } else {
                        view.loadUrl(url);
                        return true;
                    }
                }
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                CookieManager cookieManager = CookieManager.getInstance();
                String cookieStr = cookieManager.getCookie(url);
                CookieSyncManager.createInstance(getActivity());
                CookieSyncManager.getInstance().startSync();
                try {
                    int x = cookieStr.indexOf("_w_tb_nick");
                    if (x != -1) {//一登陆
                        if (!isEvent) {
                            EventEntity eventEntity = new EventEntity();
                            eventEntity.type = 9999;
                            eventEntity.state = cookieStr;
                            EventBus.getDefault().post(eventEntity);
                        }
                    } else {
                        //退出
                        EventEntity eventEntity = new EventEntity();
                        eventEntity.type = 555;
                        EventBus.getDefault().post(eventEntity);
                    }
                    isEvent = false;
                } catch (Exception e) {
                }
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventEntity eventEntity) {
        if (eventEntity.type == 6666) {
            isEvent = true;
            webView.loadUrl(Const.LOGIN_URL);
        } else if (eventEntity.type == 222) {
            webView.loadUrl(Const.LOGIN_URL);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
